﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Routing;
using System.Globalization;

namespace Sample
{
public class DocumentTypeConstraint : System.Web.Routing.IRouteConstraint
{
    private static readonly System.Text.RegularExpressions.Regex regex
        = new System.Text.RegularExpressions.Regex("^(pdf|html)$");
    #region IRouteConstraint Members

    public bool Match(HttpContextBase httpContext, System.Web.Routing.Route route, string parameterName, System.Web.Routing.RouteValueDictionary values, System.Web.Routing.RouteDirection routeDirection)
    {
        bool result = true;
        if ((routeDirection == RouteDirection.IncomingRequest) && 
            (parameterName.ToLower(CultureInfo.InvariantCulture) == "type"))
        {
                
            System.Text.RegularExpressions.Match match = regex.Match( values[parameterName] as string);
            result = match.Success;
        }
        return result;
    }

    #endregion
}
}